using System;
using System.Collections;
using System.Collections.Generic;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;

namespace HR60_PanelSoft.Service
{
	public class DeviceService : CommonService
	{
		public static Device findByUuid(string uuid)
		{
			foreach (DeviceGroup group in proj.deviceGroups)
			{
				foreach (Device device in group.devices)
				{
					if (device.uuid == uuid)
						return device;
				}
			}
			return null;
		}

		public static List<Device> findListByUuid(List<string> uuids)
		{
			List<Device> list = new List<Device>();
			foreach (string uuid in uuids)
			{
				list.Add(findByUuid(uuid));
			}
			return list;
		}

		// 保存
		public static void save(Device source, Device dto)
		{
			source.nickName = dto.nickName;
			source.brand = dto.brand;
			for (int i = 0; i < source.childDevices.Count; i++)
			{
				source.childDevices[i].nickName = dto.childDevices[i].nickName;
			}
		}

		// 添加
		public static void append(Device device, DeviceGroup group)
		{
			group.devices.Add (device);
		}
		public static Device append(DeviceType type, DeviceGroup group)
		{
			Device dev = DeviceTypeService.getDeviceCreator().create(type);
			group.devices.Add(dev);
			return dev;
		}

		// 删除
		public static void remove(Device device)
		{
			// 卸载映射关系
			if (device.type.subDeviceType != null)
			{
				foreach (Device subdev in device.childDevices)
				{
					resetByRemoveDevice (subdev);
				}
			}
			else
			{
				resetByRemoveDevice (device);
			}

			// 从设备组中删除
			foreach (DeviceGroup dg in proj.deviceGroups)
			{
				if (dg.devices.Contains (device))
				{
					dg.devices.Remove (device);
					return;
				}
			}
		}

		// 添加新子设备
		public static Device appendNewSub(Device device)
		{
			Device sub = new Device();
			sub.nickName = "分机" + (device.childDevices.Count + 1);
			sub.type = device.type.subDeviceType;
			device.childDevices.Add(sub);
			return sub;
		}

		// 删除子设备
		public static void removeSub(Device device, Device sub)
		{
			// 卸载映射关系
			resetByRemoveDevice (sub);

			// 从设备组中删除
			device.childDevices.Remove (sub);
		}

		// 卸载负载及场景关系
		private static void resetByRemoveDevice(Device device)
		{
			// 卸载场景关系
			foreach (Scene scene in proj.scenes)
			{
				foreach(SceneEvent evet in scene.events)
				{
					if (evet.getDevice() == device)
						scene.events.Remove (evet);
				}
			}

			// 卸载外部信号关系
			if (device.type.canButtonCtrl && device.connectedPins.Count > 0)
			{
				Panel pinPanel = device.getConnnectedPins () [0].getPanel ();
				if (pinPanel.type.hasSignalModel && pinPanel.signal.sconst != SignalConst.NULL)
				{
					Scene scene = SceneService.findByUuid(pinPanel.signal.scene);
					foreach(SceneEvent evet in scene.events)
					{
						if (evet.getDevice() == device)
							scene.events.Remove(evet);
					}
				}
			}

			// 卸载按键映射关系
			foreach(HR60_PanelSoft.Model.Net net in proj.nets)
			{
				foreach (Panel panel in net.panels)
				{
					foreach (PanelPage page in panel.pages)
					{
						foreach (PanelButton btn in page.buttons)
						{
							if (btn.getMappingDevice() == device) 
							{
								btn.mappingDevice = null;
							}
						}
					}

					// 卸载高级页面关系
					ExPanelPage exp = panel.getExPageByType(device.type.exType);
					if (exp != null && exp.getMappingSubExDevice().Contains (device))
					{
						exp.mappingSubExDevice.Remove (device.uuid);
					}
				}
			}

			// 卸载负载关系
			foreach (Pin pin in device.getConnnectedPins())
			{
				pin.connectedDevice = null;
				pin.isMainPin = false;
			}
			device.connectedPins.Clear ();
		}
	
	}
}

